package cn.edu.zut.internship.service.impl.report;

import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.bean.sys.SysUser;
import cn.edu.zut.internship.iservice.notify.NotifySV;
import cn.edu.zut.internship.iservice.report.ReportSV;
import cn.edu.zut.internship.iservice.student.StudentSV;
import cn.edu.zut.internship.iservice.teach.TeachSV;
import cn.edu.zut.internship.mapper.report.ReportMapper;
import cn.edu.zut.internship.mapper.setting.SettingMapper;
import cn.edu.zut.internship.mapper.teach.TeachMapper;
import cn.edu.zut.internship.utils.PrimaryKeyUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ReportSVImpl implements ReportSV {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    PrimaryKeyUtil primaryKeyUtil;
    @Autowired
    SettingMapper settingMapper;
    @Autowired
    NotifySV notifySV;
    @Autowired
    TeachMapper teachMapper;
    @Override
    public Result getReportById(Long reportId) {
        Result result = new Result();
        Map report = reportMapper.getReport(reportId);
        result.setBean(report);
        return result;
    }
    @Override
    public Result addReport(Map params){
        Result result = new Result();
        Long reportId = primaryKeyUtil.generatePrimaryKey("report");
        params.put("reportId",reportId);
        reportMapper.addReport(params);
        return result;
    }
    @Override
    public Result selectReport(Map params) {
        Result result = new Result();
        //获取当前学期
        Map semester = settingMapper.getSetting("currentSemester");
        String nowSemester = (String) semester.get("settingValue");
        params.put("semester",nowSemester);
        List<Map> list = reportMapper.getList(params);
        for (Map report : list){
            int reportType = (int) report.get("reportType");
            int reportLevel = (int) report.get("reportLevel");
            if(reportType==0){
                report.put("reportType","周报");
                report.put("reportLevel","第"+reportLevel+"份周报");
            }else if(reportType == 1){
                report.put("reportType","月报");
                report.put("reportLevel","第"+reportLevel+"份月报");
            }else if(reportType == 2){
                report.put("reportType","中期报告");
                report.put("reportLevel","第"+reportLevel+"份中期报告");
            }else if(reportType == 3){
                report.put("reportType","实习实训论文");
                report.put("reportLevel","第"+reportLevel+"份实习实训论文");
            }
            int reportStatus = (int) report.get("reportStatus");
            if(reportStatus==0){
                report.put("reportStatus","待审核");
            }else if(reportStatus == 1){
                report.put("reportStatus","审核通过");
            }else if(reportStatus == 2){
                report.put("reportStatus","驳回");
            }
            int contentType = (int) report.get("contentType");
            if(contentType==0){
                report.put("contentTypeName","在线填写");
            }else if(contentType == 1){
                report.put("contentTypeName","提交文档");
            }else {
                report.put("contentTypeName","未定义");
            }
        }
        int total = reportMapper.getListCount(params);
        result.getBean().put("total",total);
        result.setBeans(list);
        return result;
    }

    @Override
    public Result getVerifyReport(Map params) {
        Result result = new Result();
        //获取当前学期
        Map semester = settingMapper.getSetting("currentSemester");
        String nowSemester = (String) semester.get("settingValue");
        params.put("semester",nowSemester);
        List<Map> list = reportMapper.getVerifyReport(params);
        for (Map report : list){
            int reportType = (int) report.get("reportType");
            if(reportType==0){
                report.put("reportType","周报");
            }else if(reportType == 1){
                report.put("reportType","月报");
            }else if(reportType == 2){
                report.put("reportType","中期报告");
            }else if(reportType == 3){
                report.put("reportType","实习实训论文");
            }
            int contentType = (int) report.get("contentType");
            if(contentType==0){
                report.put("contentTypeName","在线填写");
            }else if(contentType == 1){
                report.put("contentTypeName","提交文档");
            }else {
                report.put("contentTypeName","未定义");
            }
        }
        int total = reportMapper.getVerifyReportCount(params);
        result.getBean().put("total",total);
        result.setBeans(list);
        return result;
    }

    @Override
    public Result delectReport(Long reportId) {
        Result result = new Result();
        reportMapper.deleteReport(reportId);
        return result;
    }

    @Override
    public Result updateReport(Map params) {
        Result result = new Result();
        reportMapper.updateReport(params);
        return result;
    }

    @Override
    public Result getMuReport(Map params) {
        return null;
    }

    @Override
    public Result getTaskReport(Map params) {
        return null;
    }


    @Override
    public Result rejectReport(Map params) {
        Result result = new Result();
        params.put("reportStatus",2);
        reportMapper.updateReport(params);
        return result;
    }

    @Override
    public Result getReportProgress(Map params) {
        Result result = new Result();
        params.put("reportType",0);
        BigDecimal weekNum = new BigDecimal(reportMapper.getListCount(params)*100);
        params.put("reportType",1);
        BigDecimal monthNum = new BigDecimal(reportMapper.getListCount(params)*100);
        params.put("reportType",2);
        BigDecimal centreNum = new BigDecimal(reportMapper.getListCount(params)*100);
        params.put("reportType",3);
        BigDecimal finalNum = new BigDecimal(reportMapper.getListCount(params)*100);

        Map map1 = settingMapper.getSetting("weekNum");
        String oldWeekNums = (String) map1.get("settingValue");
        BigDecimal oldweekNumi = new BigDecimal(oldWeekNums);

        Map map2 = settingMapper.getSetting("monthNum");
        String oldMonthNums = (String) map2.get("settingValue");
        BigDecimal oldMonthNumi = new BigDecimal(oldMonthNums);

        Map map3 = settingMapper.getSetting("centreNum");
        String oldCentreNums = (String) map3.get("settingValue");
        BigDecimal oldCentreNumi = new BigDecimal(oldCentreNums);

        Map map4 = settingMapper.getSetting("finalNum");
        String oldfinalNums = (String) map4.get("settingValue");
        BigDecimal oldfinalNumi = new BigDecimal(oldfinalNums);

        result.getBean().put("week",weekNum.divide(oldweekNumi,0,BigDecimal.ROUND_HALF_UP).intValue());
        result.getBean().put("month",monthNum.divide(oldMonthNumi,0,BigDecimal.ROUND_HALF_UP).intValue());
        result.getBean().put("centre",centreNum.divide(oldCentreNumi,0,BigDecimal.ROUND_HALF_UP).intValue());
        result.getBean().put("final",finalNum.divide(oldfinalNumi,0,BigDecimal.ROUND_HALF_UP).intValue());
        return result;
    }

    @Override
    public Result getMyReportNum(Map params) {
        Result result = new Result();
        int reportType = (int) params.get("reportType");
        int myReportNum = reportMapper.getMyReportNum(params);
        Map setting;
        if(reportType==0){
            setting = settingMapper.getSetting("weekNum");
        }else if(reportType==1){
            setting = settingMapper.getSetting("monthNum");
        }else if(reportType==2){
            setting = settingMapper.getSetting("centreNum");
        }else{
            setting = settingMapper.getSetting("finalNum");
        }
        int relNum = Integer.parseInt((String) setting.get("settingValue"));
        result.getBean().put("myReportNum",myReportNum);
        result.getBean().put("relNum",relNum);
        return result;
    }

    @Override
    public Result remindVerifyReport(Map params) {
        Result result = new Result();
        SysUser userInfo = (SysUser) params.get("userInfo");
        //获取学生指导教师信息
        Map myTeach = teachMapper.getMyTeach(userInfo.getUserAccount());
        //提醒教师审核报告
        Map notify = new HashMap();
        notify.put("sendAccount",userInfo.getUserAccount());
        notify.put("sendType",0);
        notify.put("notifyType",2);
        notify.put("reportId",params.get("reportId"));
        notify.put("content","学生"+userInfo.getUserName()+"提醒您及时审核报告");
        notify.put("targetAccount",myTeach.get("teachAccount"));
        notify.put("targetType",0);
        notify.put("creatAccount",userInfo.getUserAccount());
        notifySV.addNotify(notify);
        return result;
    }
}
